var keystone = require('keystone');
var Types = keystone.Field.Types;

/**
 * User Model
 * ==========
 */
var User = new keystone.List('User', { usePushEach: true });

User.add({
  name: { type: String, required: true, index: true },
  username: { type: String, required: false, default: "未设置昵称", index: true },
  phone: { type: String, required: false, index: true },
  email: { type: Types.Email, initial: true, required: false },
  password: { type: Types.Password, initial: true, required: false },
  createdAt: { type: Types.Datetime, index: true, default: Date.now },
  last_access: { type: Types.Datetime, index: true, default: Date.now },

}, 'Permissions', {
    isAdmin: { type: Boolean, label: 'Can access Keystone' },
  });


User.schema.set("usePushEach", true);
// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function () {
  return this.isAdmin;
});

/**
 * Relationships
 */
User.relationship({ ref: 'Post', path: 'posts', refPath: 'author' });


/**
 * Registration
 */
User.defaultColumns = 'name, phone, email, isAdmin';
User.register();

